﻿namespace HutongGames.PlayMaker
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using UnityEngine;

    public class FsmLog
    {
        [CompilerGenerated]
        private static bool CollapseSystemEvents__BackingField;
        [CompilerGenerated]
        private static bool EnableDebugFlow__BackingField;
        [CompilerGenerated]
        private HutongGames.PlayMaker.Fsm Fsm__BackingField;
        [CompilerGenerated]
        private static bool LoggingEnabled__BackingField;
        [CompilerGenerated]
        private static bool MirrorDebugLog__BackingField;
        private static int currentFrame;
        private static float currentFrameTime;
        private List<FsmLogEntry> entries = new List<FsmLogEntry>();
        private static readonly List<FsmLog> Logs = new List<FsmLog>();
        private const int MaxLogSize = 0xc350;

        private FsmLog(HutongGames.PlayMaker.Fsm fsm)
        {
            this.Fsm = fsm;
        }

        private void AddEntry(FsmLogEntry entry)
        {
            entry.FrameCount = Time.frameCount;
            entry.Time = FsmTime.RealtimeSinceStartup;
            if (!string.IsNullOrEmpty(entry.Text))
            {
                entry.Text = FormatTime(entry.Time) + " " + entry.Text;
            }
            this.entries.Add(entry);
            if (this.entries.Count > 0xc350)
            {
                this.entries.RemoveRange(0, 100);
            }
            switch (entry.LogType)
            {
                case FsmLogType.Warning:
                    Debug.LogWarning(this.FormatUnityLogString(entry.Text));
                    return;

                case FsmLogType.Error:
                    Debug.LogError(this.FormatUnityLogString(entry.Text));
                    return;
            }
            if (MirrorDebugLog)
            {
                Debug.Log(this.FormatUnityLogString(entry.Text));
            }
        }

        public void Clear()
        {
            if (this.entries != null)
            {
                this.entries.Clear();
            }
        }

        public static void ClearLogs()
        {
            foreach (FsmLog log in Logs)
            {
                log.Clear();
            }
        }

        public static string FormatTime(float time)
        {
            DateTime time2 = new DateTime((long) (time * 1E+07f));
            return time2.ToString("mm:ss:ff");
        }

        private string FormatUnityLogString(string text)
        {
            string fullFsmLabel = HutongGames.PlayMaker.Fsm.GetFullFsmLabel(this.Fsm);
            if (HutongGames.PlayMaker.Fsm.ExecutingState != null)
            {
                fullFsmLabel = fullFsmLabel + " : " + HutongGames.PlayMaker.Fsm.ExecutingState.Name;
            }
            return (fullFsmLabel + " : " + text);
        }

        public static FsmLog GetLog(HutongGames.PlayMaker.Fsm fsm)
        {
            if (fsm == null)
            {
                return null;
            }
            foreach (FsmLog log in Logs)
            {
                if (log.Fsm == fsm)
                {
                    return log;
                }
            }
            FsmLog item = new FsmLog(fsm);
            Logs.Add(item);
            return item;
        }

        public void Log(string text)
        {
            this.Log(FsmLogType.Info, text);
        }

        public void Log(FsmLogType logType, string text)
        {
            FsmLogEntry entry2 = new FsmLogEntry();
            entry2.LogType = logType;
            entry2.State = HutongGames.PlayMaker.Fsm.ExecutingState;
            entry2.Text = text;
            FsmLogEntry entry = entry2;
            this.AddEntry(entry);
        }

        public void LogAction(FsmLogType logType, string text)
        {
            if (HutongGames.PlayMaker.Fsm.ExecutingAction != null)
            {
                FsmLogEntry entry2 = new FsmLogEntry();
                entry2.LogType = logType;
                entry2.State = HutongGames.PlayMaker.Fsm.ExecutingState;
                entry2.Action = HutongGames.PlayMaker.Fsm.ExecutingAction;
                entry2.Text = FsmUtility.StripNamespace(HutongGames.PlayMaker.Fsm.ExecutingAction.ToString()) + " : " + text;
                FsmLogEntry entry = entry2;
                this.AddEntry(entry);
            }
        }

        public void LogBreak()
        {
            FsmLogEntry entry2 = new FsmLogEntry();
            entry2.LogType = FsmLogType.Break;
            entry2.State = HutongGames.PlayMaker.Fsm.ExecutingState;
            entry2.Text = "BREAK";
            FsmLogEntry entry = entry2;
            Debug.Log(this.FormatUnityLogString("Breakpoint"));
            this.AddEntry(entry);
        }

        public void LogEnterState(FsmState state)
        {
            if (state != null)
            {
                FsmLogEntry entry2 = new FsmLogEntry();
                entry2.LogType = FsmLogType.EnterState;
                entry2.State = state;
                entry2.Text = "ENTER: " + state.Name;
                FsmLogEntry entry = entry2;
                if (EnableDebugFlow && state.Fsm.EnableDebugFlow)
                {
                    entry.FsmVariablesCopy = new FsmVariables(state.Fsm.Variables);
                    entry.GlobalVariablesCopy = new FsmVariables(FsmVariables.GlobalVariables);
                }
                this.AddEntry(entry);
            }
        }

        public void LogError(string text)
        {
            this.Log(FsmLogType.Error, text);
        }

        public void LogEvent(FsmEvent fsmEvent, FsmState state)
        {
            if ((!CollapseSystemEvents || !fsmEvent.IsSystemEvent) || ((this.entries.Count <= 0) || (this.entries[this.entries.Count - 1].Event != fsmEvent)))
            {
                FsmLogEntry entry2 = new FsmLogEntry();
                entry2.LogType = FsmLogType.Event;
                entry2.State = state;
                entry2.SentByState = HutongGames.PlayMaker.Fsm.EventData.SentByState;
                entry2.Action = HutongGames.PlayMaker.Fsm.EventData.SentByAction;
                entry2.Event = fsmEvent;
                entry2.Text = "EVENT: " + fsmEvent.Name;
                FsmLogEntry entry = entry2;
                this.AddEntry(entry);
            }
        }

        public void LogExitState(FsmState state)
        {
            if (state != null)
            {
                FsmLogEntry entry2 = new FsmLogEntry();
                entry2.LogType = FsmLogType.ExitState;
                entry2.State = state;
                entry2.Text = "EXIT: " + state.Name;
                FsmLogEntry entry = entry2;
                if ((EnableDebugFlow && state.Fsm.EnableDebugFlow) && !PlayMakerFSM.ApplicationIsQuitting)
                {
                    entry.FsmVariablesCopy = new FsmVariables(state.Fsm.Variables);
                    entry.GlobalVariablesCopy = new FsmVariables(FsmVariables.GlobalVariables);
                }
                this.AddEntry(entry);
            }
        }

        public void LogSendEvent(FsmState state, FsmEvent fsmEvent, FsmEventTarget eventTarget)
        {
            if (((state != null) && (fsmEvent != null)) && !fsmEvent.IsSystemEvent)
            {
                FsmLogEntry entry2 = new FsmLogEntry();
                entry2.LogType = FsmLogType.SendEvent;
                entry2.State = state;
                entry2.Event = fsmEvent;
                entry2.Text = "SEND EVENT: " + fsmEvent.Name;
                entry2.EventTarget = new FsmEventTarget(eventTarget);
                FsmLogEntry entry = entry2;
                this.AddEntry(entry);
            }
        }

        public void LogStart(FsmState startState)
        {
            FsmLogEntry entry2 = new FsmLogEntry();
            entry2.LogType = FsmLogType.Start;
            entry2.State = startState;
            entry2.Text = "START";
            FsmLogEntry entry = entry2;
            this.AddEntry(entry);
        }

        public void LogTransition(FsmState fromState, FsmTransition transition)
        {
            FsmLogEntry entry2 = new FsmLogEntry();
            entry2.LogType = FsmLogType.Transition;
            entry2.State = fromState;
            entry2.Transition = transition;
            FsmLogEntry entry = entry2;
            this.AddEntry(entry);
        }

        public void LogWarning(string text)
        {
            this.Log(FsmLogType.Warning, text);
        }

        public void OnDestroy()
        {
            Logs.Remove(this);
            this.Clear();
            this.entries = null;
            this.Fsm = null;
        }

        public static bool CollapseSystemEvents
        {
            [CompilerGenerated]
            get
            {
                return CollapseSystemEvents__BackingField;
            }
            [CompilerGenerated]
            set
            {
                CollapseSystemEvents__BackingField = value;
            }
        }

        public static bool EnableDebugFlow
        {
            [CompilerGenerated]
            get
            {
                return EnableDebugFlow__BackingField;
            }
            [CompilerGenerated]
            set
            {
                EnableDebugFlow__BackingField = value;
            }
        }

        public List<FsmLogEntry> Entries
        {
            get
            {
                return this.entries;
            }
        }

        public HutongGames.PlayMaker.Fsm Fsm
        {
            [CompilerGenerated]
            get
            {
                return this.Fsm__BackingField;
            }
            [CompilerGenerated]
            private set
            {
                this.Fsm__BackingField = value;
            }
        }

        public static bool LoggingEnabled
        {
            [CompilerGenerated]
            get
            {
                return LoggingEnabled__BackingField;
            }
            [CompilerGenerated]
            set
            {
                LoggingEnabled__BackingField = value;
            }
        }

        public static bool MirrorDebugLog
        {
            [CompilerGenerated]
            get
            {
                return MirrorDebugLog__BackingField;
            }
            [CompilerGenerated]
            set
            {
                MirrorDebugLog__BackingField = value;
            }
        }
    }
}

